const Db = require('./db')
const vk = require('./vk')

class system
{
  constructor(){
  }
  /**
   *
   * 获取权限规则
   * @memberof system
   */
  static async menus(ctx){
    let data = {
      sql: "SELECT * FROM vk_system_menus ORDER BY sort DESC"
    }
    let [err,res] = await Db.query(data)
    if(!err){
      ctx.body = vk.showData(200,res,'ok')
    }else{
      ctx.body = vk.showData(500,[],err)
    }
  }

  /**
   *
   * 添加栏目
   * @static
   */
  static async addMenu(ctx){
    let params = ctx.request.body
    let checkNameData = {
      sql:"SELECT name FROM vk_system_menus WHERE name = ?",
      values:[params.name]
    }
    let [err,res] = await Db.query(checkNameData)
    if(err){return ctx.body = vk.showData(500,[],err)}
    if(JSON.stringify(res) === "[]"){
      if(params.level != 2){
        let level = parseInt(params.level) + 1
        let data = {
          sql:"INSERT INTO vk_system_menus (title,pid,path,name,icon,is_show,sort,level) VALUES (?,?,?,?,?,?,?,?)",
          values:[params.title,params.pid,params.path,params.name,params.icon,params.is_show,params.sort,level]
        }
        let [err2,res2] = await Db.query(data)
        if(err2){
          return ctx.body = vk.showData(500,err2,'添加失败')
        }
        ctx.body = vk.showData(200,res2,'栏目创建成功')
      }else{
        ctx.body = vk.showData(500,[],'二级栏目不可再添加')
      }
    }else{
      ctx.body = vk.showData(500,[],'标识重复')
    }
  }

  /**
   *
   * 修改栏目
   * @static
   * @memberof system
   */
  static async editMenu(ctx){
    let params = ctx.request.body
    if(params.method === "edit"){
      if(params.name!=''&&params.title!=''){
        let checkData = { // 判断name是否重复
          sql:"SELECT name FROM vk_system_menus WHERE id != ? AND name = ?",
          values:[params.id,params.name]
        }
        let [err2,res2] = await Db.query(checkData)
        if(err2){return ctx.body = vk.showData(500,err2,'修改失败')}
        if(JSON.stringify(res2) == "[]"){
          let data = {
            sql:"UPDATE vk_system_menus SET title=?,path=?,name=?,icon=?,is_show=?,sort=? WHERE id=?",
            values:[params.title,params.path,params.name,params.icon,params.is_show,params.sort,params.id]
          }
          let [err,res] = await Db.query(data)
          if(err){return ctx.body = vk.showData(500,err,'修改失败')}
          ctx.body = vk.showData(200,res,'栏目修改成功')
        }else{
          ctx.body = vk.showData(500,[],'标识重复')
        }
      }else{
        ctx.body = vk.showData(500,[],'缺少参数')
      }
    }else{
      let data = {
        sql: "UPDATE vk_system_menus SET is_show=? WHERE name=?",
        values:[params.is_show,params.name]
      }
      let [err,res] = await Db.query(data)
      if(!err){
        ctx.body = vk.showData(200,res,'ok')
      }else{
        ctx.body = vk.showData(500,[],err)
      }
    }
  }

  /**
   *
   * 删除栏目
   * @static
   * @memberof system
   */
  static async delMenu(ctx){
    let params = ctx.request.body
    let data = {
      sql:"DELETE FROM vk_system_menus WHERE id=?",
      values:[params.id]
    }
    let [err] = await Db.query(data)
    if(err){
      return ctx.body = vk.showData(500,[],err)
    }
    ctx.body = vk.showData(200,[],"删除成功")
  }

  /**
   *
   * 获取权限规则
   * @static
   * @memberof system
   */
  static async getRules(ctx){
    let data = {
      sql:"SELECT * FROM vk_admin_authority_rule"
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,"请求失败")}
    ctx.body = vk.showData(200,res,"ok")
  }

  /**
   *
   * 获取权限列表
   * @static
   * @memberof system
   */
  static async getAuthorityList(ctx){
    let data = {
      sql:"SELECT * FROM vk_admin_authority_list"
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,"请求失败")}
    ctx.body = vk.showData(200,res,"ok")
  }

  /**
   *
   * 添加权限
   * @static
   * @memberof system
   */
  static async addAuthority(ctx){
    let params = ctx.request.body;
    let checkName = {
      sql:"SELECT name FROM vk_admin_authority_list WHERE name = ?",
      values:[params.name]
    }
    let [errc,resc] = await Db.query(checkName)
    if(errc){return ctx.body = vk.showData(500,err,"请求失败")}
    if(JSON.stringify(resc) == "[]"){
      let data = {
        sql:"INSERT INTO vk_admin_authority_list (`title`,`name`,`pid`,`type`,`url`,`is_use`) VALUES (?,?,?,?,?,?)",
        values:[params.title,params.name,params.pid,params.type,params.url,params.is_use]
      }
      let [err,res] = await Db.query(data)
      if(err){return ctx.body = vk.showData(500,err,"请求失败")}
      ctx.body = vk.showData(200,res,"ok")
    }else{
      ctx.body = vk.showData(500,[],"标识重复")
    }
  }

  /**
   *
   * 修改权限
   * @static
   * @memberof system
   */
  static async editAuthority(ctx){
    let params = ctx.request.body;
    let data = {}
    if(params.method === "edit"){
      data = {
        sql:"UPDATE vk_admin_authority_list SET `pid` = ?,`title` = ?,`name` = ?,`type` = ?,`url` = ?,`is_use` = ? WHERE id = ?",
        values:[params.pid,params.title,params.name,params.type,params.url,params.is_use,params.id]
      }
    }else{
      data = {
        sql:"UPDATE vk_admin_authority_list SET is_use = ? WHERE id = ?",
        values:[params.is_use,params.id]
      }
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,"请求失败")}
    ctx.body = vk.showData(200,res,"ok")
  }

  /**
   *
   * 删除权限
   * @static
   * @memberof system
   */
  static async delAuthority(ctx){
    let params = ctx.request.body
    let data = {
      sql:"DELETE FROM vk_admin_authority_list WHERE id = ?",
      values:[params.id]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,"请求失败")}
    ctx.body = vk.showData(200,res,"ok")
  }

  /**
   *
   * 添加规则
   * @static
   * @memberof system
   */
  static async addRules(ctx){
    let params = ctx.request.body
    let data = {
      sql:"INSERT INTO vk_admin_authority_rule SET `title` = ?,rules = ?,is_use = ?",
      values:[params.title,params.rules,params.is_use]
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,"请求失败")}
    ctx.body = vk.showData(200,res,"ok")
  }

  /**
   *
   * 删除规则
   * @static
   * @memberof system
   */
  static async delRules(ctx){
    let params = ctx.request.body
    let data = {
      sql:"DELETE FROM vk_admin_authority_rule WHERE id = ?",
      values:[params.id]
    }
    let [err,res] = await Db.query(data)
    console.log(params)
    if(err){return ctx.body = vk.showData(500,err,"请求失败")}
    ctx.body = vk.showData(200,res,"ok")
  }

  /**
   *
   * 修改规则
   * @static
   * @memberof system
   */
  static async editRules(ctx){
    let params = ctx.request.body
    let data = {}
    if(params.method === "edit"){
      data = {
        sql:"UPDATE vk_admin_authority_rule SET title = ?,is_use = ?,rules = ? WHERE id = ?",
        values:[params.title,params.is_use,params.rules,params.id]
      }
    }else{
      data = {
        sql:"UPDATE vk_admin_authority_rule SET is_use = ? WHERE id = ?",
        values:[params.is_use,params.id]
      }
    }
    let [err,res] = await Db.query(data)
    if(err){return ctx.body = vk.showData(500,err,"请求失败")}
    ctx.body = vk.showData(200,res,"ok")
  }
}

module.exports = system;